<template>
  <div class="system-config">
    <div class="page-header">
      <h2>系统配置</h2>
      <button class="btn btn-primary" @click="loadConfig">🔄 刷新</button>
    </div>

    <div v-if="loading" class="loading">加载中...</div>

    <div v-else class="config-sections">
      <!-- 基础信息配置 -->
      <div class="card">
        <h3 class="section-title">基础信息</h3>
        <div class="config-list">
          <div class="config-item">
            <label>系统名称</label>
            <input v-model="configData.system_name" type="text" />
          </div>
          <div class="config-item">
            <label>系统版本</label>
            <input v-model="configData.system_version" type="text" />
          </div>
          <div class="config-item">
            <label>系统描述</label>
            <textarea v-model="configData.system_description" rows="3"></textarea>
          </div>
        </div>
      </div>

      <!-- 功能开关 -->
      <div class="card">
        <h3 class="section-title">功能开关</h3>
        <div class="config-list">
          <div class="config-item">
            <label>启用游客模式</label>
            <select v-model="configData.guest_mode_enabled">
              <option :value="true">是</option>
              <option :value="false">否</option>
            </select>
          </div>
          <div class="config-item">
            <label>启用自动发货</label>
            <select v-model="configData.auto_delivery_enabled">
              <option :value="true">是</option>
              <option :value="false">否</option>
            </select>
          </div>
        </div>
      </div>

      <!-- 客服信息 -->
      <div class="card">
        <h3 class="section-title">客服信息</h3>
        <div class="config-list">
          <div class="config-item">
            <label>客服姓名</label>
            <input v-model="configData.customer_service_name" type="text" />
          </div>
          <div class="config-item">
            <label>客服电话</label>
            <input v-model="configData.customer_service_phone" type="text" />
          </div>
          <div class="config-item">
            <label>客服微信号</label>
            <input v-model="configData.customer_service_wechat" type="text" />
          </div>
          <div class="config-item">
            <label>客服QQ</label>
            <input v-model="configData.customer_service_qq" type="text" />
          </div>
          <div class="config-item">
            <label>客服邮箱</label>
            <input v-model="configData.customer_service_email" type="email" />
          </div>
          <div class="config-item">
            <label>工作时间</label>
            <input v-model="configData.customer_service_work_time" type="text" />
          </div>
          <div class="config-item">
            <label>客服二维码URL</label>
            <input v-model="configData.customer_service_qr" type="text" />
          </div>
        </div>
      </div>

      <!-- 保存按钮 -->
      <div class="card">
        <button class="btn btn-primary btn-large" @click="saveConfig">💾 保存所有配置</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getConfig, batchUpdateConfig } from '../api/config';

const loading = ref(false);
const configData = ref({
  system_name: '',
  system_version: '',
  system_description: '',
  guest_mode_enabled: true,
  auto_delivery_enabled: true,
  customer_service_name: '',
  customer_service_phone: '',
  customer_service_wechat: '',
  customer_service_qq: '',
  customer_service_email: '',
  customer_service_work_time: '',
  customer_service_qr: ''
});

const loadConfig = async () => {
  loading.value = true;
  try {
    const res = await getConfig();
    if (res.success) {
      configData.value = { ...configData.value, ...res.data };
    } else {
      alert(res.message || '加载失败');
    }
  } catch (error) {
    console.error('加载配置失败:', error);
    alert('网络错误');
  } finally {
    loading.value = false;
  }
};

const saveConfig = async () => {
  if (!confirm('确定要保存这些配置吗？')) return;

  try {
    const configs = Object.keys(configData.value).map(key => ({
      key,
      value: configData.value[key]
    }));

    const res = await batchUpdateConfig(configs);
    if (res.success) {
      alert('保存成功！');
      loadConfig();
    } else {
      alert(res.message || '保存失败');
    }
  } catch (error) {
    console.error('保存配置失败:', error);
    alert('网络错误');
  }
};

onMounted(() => {
  loadConfig();
});
</script>

<style scoped>
.system-config {
  max-width: 100%;
}

.config-sections {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.section-title {
  margin: 0 0 20px 0;
  font-size: 18px;
  font-weight: 600;
  padding-bottom: 12px;
  border-bottom: 2px solid #e23;
}

.config-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.config-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.config-item label {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.config-item input,
.config-item textarea,
.config-item select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #e5e5e5;
  border-radius: 6px;
  font-size: 14px;
  box-sizing: border-box;
}

.config-item textarea {
  resize: vertical;
}

.btn-large {
  width: 100%;
  padding: 16px;
  font-size: 16px;
}
</style>

